/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databook;

import databook.controller.Props;
import databook.help.helpFrame;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


/**
 *
 * @author zach
 */
public class Main {

    public static ControlPanel controls;
    public static DatabookViz viz;
    
    private static JFrame frame;
    private static helpFrame[] helpFrames;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
	        
        //build the menu
    	
    	
    	frame = new JFrame("UMBC Databook Visualization - Beta");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        helpFrames = new helpFrame[helpFrame.NUMBER];
    
    	JMenuBar menuBar = new JMenuBar();
        
        frame.setJMenuBar(menuBar);
        
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(helpMenu);
        
        JMenuItem newAction = new JMenuItem("New");
        newAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("You have clicked on the new action");
            }
        });
        
        JMenuItem openAction = new JMenuItem("Open");
        openAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                openMethod();
            }
        });
        
        JMenuItem saveAction = new JMenuItem("Save");
        saveAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                saveMethod();
            }
        });
        
        
        JMenuItem exitAction = new JMenuItem("Exit");
        exitAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	System.exit(0);
            }
        });
        
        
        JMenuItem diagramAction = new JMenuItem("General");
        diagramAction.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		helpMethod(helpFrame.DIAGRAM);
        	}
        });
        JMenuItem detailAction = new JMenuItem("Details Tab");
        detailAction.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		helpMethod(helpFrame.DETAILS);
        	}
        });
        JMenuItem filterAction = new JMenuItem("Filtering Tab");
        filterAction.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		helpMethod(helpFrame.FILTER);
        	}
        });
        
        
        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.add(saveAction);        
        fileMenu.addSeparator();
        fileMenu.add(exitAction);

        helpMenu.add(diagramAction);
        helpMenu.add(filterAction);
        helpMenu.add(detailAction);
        
        //Set up the content pane.
        
        JPanel panel = new JPanel();
        
        panel.setLayout(new FlowLayout());
        
        viz = new DatabookViz();

        panel.add(viz);
        
        controls = new ControlPanel(viz.nodeTree);
        //viz.setFilter(controls);
        
        panel.add(controls);
        
        frame.setContentPane(panel);
        
        frame.setSize(900, 600);
        
        frame.pack();
        frame.setVisible(true);
    }
    
    /**
     * create JFileChooser to select file to save as
     */
    private static void saveMethod()
    {
    	JFileChooser chooser = new JFileChooser();
    	chooser.showSaveDialog(frame);

    	// Get the selected file
        File file = chooser.getSelectedFile();
        
        //null file means cancelled by user
        if(file != null)
        {
            try {
                Props.save(file);
            } catch (IOException ioe) {
                System.err.println("Error writing to file " + file + ": " + ioe);
            }
        }
    }
    
    /**
     * 
     */
    private static void openMethod()
    {
    	JFileChooser chooser = new JFileChooser();
    	chooser.showOpenDialog(frame);

    	// Get the selected file
        File file = chooser.getSelectedFile();

        //null file means cancelled by user
        if(file != null)
        {
            try {
                Props.load(file);
            } catch (FileNotFoundException fnfe) {
                System.err.println("Could not open file " + file + ": " + fnfe);
            } catch (IOException ioe) {
                System.err.println("Error reading file " + file + ": " + ioe);
            }
        }
    }
 
    
    private static void helpMethod(int type){
    	if ((type <= helpFrame.NUMBER) && (helpFrames[type] == null)) {
    		helpFrames[type] = new helpFrame(type);
    	}
    	helpFrames[type].setVisible(true);
    	
    } 
}
